package com.siwei.mes.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 *
 * @author linzi
 * @create 2022-02-25 16:48
 */
@Slf4j
public class RegexUtils {
    /**
     * 验证Email
     *
     * @param email email地址，格式：zhangsan@zuidaima.com，zhangsan@xxx.com.cn，xxx代表邮件服务商
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkEmail(String email) {
        String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
        return Pattern.matches(regex, email);
    }

    /**
     * 验证身份证号码
     *
     * @param idCard 居民身份证号码15位或18位，最后一位可能是数字或字母
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkIdCard(String idCard) {
        String regex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
        return Pattern.matches(regex, idCard);
    }

    /**
     * 验证手机号码
     */
    public static boolean checkPhone(String mobile) {
        String regex = "(\\+\\d+)?1[345789]\\d{9}$";
        return Pattern.matches(regex, mobile);
    }

    /**
     * 验证日期（年月日）
     *
     * @param birthday 日期，格式：1992-09-03，或1992.09.03
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkBirthday(String birthday) {
        String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
        return Pattern.matches(regex, birthday);
    }

    /**
     * 验证网址
     *
     * @param url 网址  www.baidu.com
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkUrl(String url) {
//        String regex = "^([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"; //这个是不判断后面的域名地址
        String regex = "^([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?\\.(com|cn|net|org|biz|info|cc|tv)";
        return Pattern.matches(regex, url);
    }

    /**
     * 验证是不是正数
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        String regex = "[0-9]+\\.?[0-9]*";
        return Pattern.matches(regex, str);
    }

    /**
     * 验证经度
     *
     * @param number
     * @return
     */
    public static boolean isLng(Double number) {
        String regex = "/^[\\-\\+]?(0(\\.\\d{1,10})?|([1-9](\\d)?)(\\.\\d{1,10})?|1[0-7]\\d{1}(\\.\\d{1,10})?|180\\.0{1,10})$/";
        return Pattern.matches(regex, String.valueOf(number));
    }


    public static void main(String[] args) {
        System.out.println("邮箱地址：" + checkEmail("374826075@qq.com"));
        System.out.println("身份证：" + checkIdCard("4305212002122556X"));
        System.out.println("手机号：" + checkPhone("18811112222"));
        System.out.println("生日：" + checkBirthday("1997-06-16"));
        System.out.println("网址：" + checkUrl("www.131231231231312.com"));
        System.out.println("整数：" + isNumber("123"));
        System.out.println("经度：" + isLng(1d));
    }
}
